﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChartSharp
{
    public abstract class BaseEncoder<TType> where TType : struct, IComparable<TType>
    {
        protected internal abstract string Prefix { get; }

        protected internal abstract string DataSetsSeparator { get; }

        protected internal abstract string NumbersSeparator { get; }

        protected abstract string MissingValue { get; }

        protected abstract TType MinValidValue { get; }

        public abstract TType MaxValidValue { get; }

        protected abstract string EncodeValue(TType value);

        public string Convert(TType? number)
        {
            if (number == null)
            {
                return MissingValue;
            }

            if (!IsRangeValid(number.Value))
            {
                throw new ArgumentOutOfRangeException("value");
            }

            return EncodeValue(number.Value);
        }

        private bool IsRangeValid(TType number)
        {
            return (number.CompareTo(MinValidValue) >= 0) && (number.CompareTo(MaxValidValue) <= 0);
        }
    }
}
